﻿using ScanConfirm.Business;
using ScanConfirm.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ScanConfirmForm
{
    public partial class FrmPartSet : Form
    {
        PartsSetupModel _PartsSetupModel;
        PartsSetupBLL _PartsSetupBLL = new PartsSetupBLL();
        public Action RefreshUiEvent;
        public FrmPartSet()
        {
            InitializeComponent();
        }

        public FrmPartSet(PartsSetupModel partsSetupModel)
        {
            InitializeComponent();
            _PartsSetupModel = partsSetupModel;
            txtCarCode.Text = partsSetupModel.CarCode;
            txtPartsCode.Text = partsSetupModel.Code;
            txtPartsPosition.Text = partsSetupModel.PartsPosition.ToString();
            txtLength.Text = partsSetupModel.Length.ToString();
            txtMark.Text = partsSetupModel.Mark;
            txtDisplayCode.Text = partsSetupModel.DisplayCode;
            txtCode.Text = partsSetupModel.PartsCode;
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCarCode.Text.Trim()) || string.IsNullOrEmpty(txtPartsCode.Text.Trim()))
            {
                MessageBox.Show("请输入车型代码和部品代码！");
                return;
            }

            if (string.IsNullOrEmpty(txtPartsPosition.Text.Trim()))
            {
                MessageBox.Show("请输入端末位！");
                return;
            }
            var partsPosition = 0;

            if (!int.TryParse(txtPartsPosition.Text.Trim(), out partsPosition))
            {
                MessageBox.Show("端末位请输入整数！");
                return;
            }

            if (string.IsNullOrEmpty(txtLength.Text.Trim()))
            {
                MessageBox.Show("请输入长度！");
                return;
            }
            var length = 0;

            if (!int.TryParse(txtLength.Text.Trim(), out length))
            {
                MessageBox.Show("长度请输入整数！");
                return;
            }

            if (_PartsSetupModel == null)
            {
                var model = _PartsSetupBLL.GetPartsSetupByCode(txtCarCode.Text);
                if(model != null)
                {
                    MessageBox.Show("该部品代码已配置，不能重复！");
                    return;
                }
                _PartsSetupModel = new PartsSetupModel();
                _PartsSetupModel.CarCode = txtCarCode.Text;
                _PartsSetupModel.PartsCode = txtCode.Text;
                _PartsSetupModel.PartsPosition = partsPosition;
                _PartsSetupModel.Length = length;
                _PartsSetupModel.Mark = txtMark.Text;
                _PartsSetupModel.DisplayCode = txtDisplayCode.Text;
                _PartsSetupModel.Code = txtPartsCode.Text;
                _PartsSetupModel.SyncFlag = 0;

                var result = _PartsSetupBLL.Add(_PartsSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加部品配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加部品配置失败：" + result.Dsca);
            }
            else
            {
                _PartsSetupModel.CarCode = txtCarCode.Text;
                _PartsSetupModel.PartsCode = txtCode.Text;
                _PartsSetupModel.PartsPosition = partsPosition;
                _PartsSetupModel.Length = length;
                _PartsSetupModel.Mark = txtMark.Text;
                _PartsSetupModel.Code = txtPartsCode.Text;
                _PartsSetupModel.DisplayCode = txtDisplayCode.Text;
                _PartsSetupModel.SyncFlag = 2;
                var result = _PartsSetupBLL.Update(_PartsSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改部品配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改部品配置失败：" + result.Dsca);
            }
        }
    }
}
